let babel = require('@babel/core');
let t = require('babel-types');
let visitor = {
    ImportDeclaration(nodePath, state) {
        console.log('state', state);//opts: { library: 'lodash' },
        let { opts } = state;
        let { node } = nodePath;
        let specifiers = node.specifiers;//[flatten,concat]
        let source = node.source;//lodash
        //只有第一个specifiers不是默认导入的话才会进来
        //如果已经 转换过了，已经 把一个普通导入变成默认导入了，那就不要进来 了
        if (opts.library === source.value && !t.isImportDefaultSpecifier(specifiers[0])) {
            const importDeclarations = specifiers.map((specifier) => {
                return t.importDeclaration([
                    t.importDefaultSpecifier(specifier.local)
                ], t.stringLiteral(`${source.value}/${specifier.imported.name}`));
            });
            if (importDeclarations.length == 1) {
                nodePath.replaceWith(importDeclarations[0]);
            } else {
                nodePath.replaceWithMultiple(importDeclarations);
            }
        }

    }
}
module.exports = function () {
    return {
        visitor
    }
}